﻿//  --------------------------------
//  Copyright (c) AW Systems, Inc. All rights reserved.
//  This source code is made available under the terms of the Microsoft Public License (Ms-PL)
//  http://www.codeplex.com/SLAM/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Threading;
using System.Web;
using Microsoft.SharePoint;
using System.IO;
using SlamContext.Cache;

namespace Slam.Configuration
{
	/// <summary>
	/// The SlamItemContext provides access to the SPListItem currently being processed by SLAM anywhere within SLAM's processing stack.
	/// </summary>
	public static class SlamItemContext
	{
		/// <summary>
		/// The SPListItem currently being processed by SLAM.
		/// </summary>
		public static SPListItem ListItem
		{
			get 
			{	
				return SlamContext.Cache.Get<SPListItem>(Constants.Cache.SlamItemContextListItem);
			}
			set
			{
				SlamContext.Cache.Add(Constants.Cache.SlamItemContextListItem, value, CacheScope.Thread);
			}
		}
		
		/// <summary>
		/// Indiciates whether the SPListItem currently being processed by SLAM is configured
		/// to be slammed based on its Content Type or parent List.
		/// </summary>
		public static bool ProcessedByContentType 
		{
			get 
			{
				if (SlamContext.Cache.ContainsKey(Constants.Cache.SlamItemContextProcessedByContentType, CacheScope.Thread))
					return SlamContext.Cache.Get<bool>(Constants.Cache.SlamItemContextProcessedByContentType);
				else
					return false;
			}
			set
			{
				SlamContext.Cache.Add(Constants.Cache.SlamItemContextProcessedByContentType, value, CacheScope.Thread);
			}
		}

		/// <summary>
		/// Indiciates whether the activity being logged for SLAM to perform using the SPListItem
		/// currentely processed should be committed.
		/// </summary>
		public static bool CommitActivity
		{
			get
			{
				//return SlamContext.Cache.Get<bool>(Constants.Cache.SlamItemContextCommitActivity);
				return true;
			}
			set
			{
				SlamContext.Cache.Add(Constants.Cache.SlamItemContextCommitActivity, value, CacheScope.Thread);
			}
		}

		/// <summary>
		/// Log for use tracing activity at all levels of SLAM's processing.  Used primarily for logging SQL statements that may or may not
		/// called as determined by the CommitActivity flag.
		/// </summary>
		public static TextWriter ActivityLog
		{
			get
			{
				return SlamContext.Cache.Get<TextWriter>(Constants.Cache.SlamItemContextActivityLog);
			}
			set
			{
				SlamContext.Cache.Add(Constants.Cache.SlamItemContextActivityLog, value, CacheScope.Thread);
			}
		}

		/// <summary>
		/// Name of the List or Content Type configured to be managed by SLAM that corresponds with the SPListItem 
		/// currently being processed.  In the case of Lists this is the SPListItem's parent List Title, in the case
		/// of Content Types this is the SPListItem's Content Type Name or the Name of a parent Content Type.
		/// </summary>
		public static string TypeName
		{
			get
			{
				return SlamContext.Cache.Get<string>(Constants.Cache.SlamItemContextTypeName);
			}
			set
			{
				SlamContext.Cache.Add(Constants.Cache.SlamItemContextTypeName, value, CacheScope.Thread);
			}
		}

		/// <summary>
		/// User LoginName of user that triggered the SLAM event.
		/// </summary>
		public static string UserLoginName
		{
			get
			{
				return SlamContext.Cache.Get<string>(Constants.Cache.SlamItemContextUserLoginName);
			}
			set
			{
				SlamContext.Cache.Add(Constants.Cache.SlamItemContextUserLoginName, value, CacheScope.Thread);
			}
		}

		/// <summary>
		/// Indicates whether folder name for folder being processed changed.
		/// </summary>
		public static bool FolderNameChanged
		{
			get
			{
				if (SlamContext.Cache.ContainsKey(Constants.Cache.SlamItemContextFolderNameChanged, CacheScope.Thread))
					return SlamContext.Cache.Get<bool>(Constants.Cache.SlamItemContextFolderNameChanged);
				else
					return false;
			}
			set
			{
				SlamContext.Cache.Add(Constants.Cache.SlamItemContextFolderNameChanged, value, CacheScope.Thread);
			}
		}

		/// <summary>
		/// Resets the SLAM Item Context by clearing its properties
		/// </summary>
		public static void ResetContext()
		{
			SlamContext.Cache.Clear(CacheScope.Thread, "t" + Thread.CurrentThread.ManagedThreadId.ToString() + "t");
		}

		static SlamItemContext()
		{
			ProcessedByContentType = false;
			CommitActivity = true;
		}
	}
}
